<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import echarts from "echarts";
import resize from "./mixins/resize";

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart",
    },
    id: {
      type: String,
      default: "chart",
    },
    width: {
      type: String,
      default: "200px",
    },
    height: {
      type: String,
      default: "200px",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  created() {},
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption({
        backgroundColor: "#fff",
        tooltip: {
          show: true,
          trigger: "item",
          backgroundColor: "rgba(0,0,0,0.5)",
          axisPointer: {
            show: false,
            lineStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "#3798FD",
                  },
                  {
                    offset: 0.5,
                    color: "#2945CB",
                  },
                  {
                    offset: 1,
                    color: "#2A67E4",
                  },
                ],
                global: false,
              },
            },
          },
        },
        legend: {
          right: 20,
          top: 0,
          itemGap: 10, // 各个item之间的间隔，单位px，默认为10，
          itemWidth: 30, // 图例图形宽度
          itemHeight: 10, // 图例图形高度
          textStyle: {
            color: "red",
            fontWeight: "normal",
            fontSize: 14,
          },
        },
        grid: {
          top: "5%",
          left: "3%",
          right: "1%",
          bottom: "8%",
        },
        tooltip: {
          trigger: "axis",
          backgroundColor: "transparent", //标题背景色
          borderColor: "#fff", //边框颜色
          borderWidth: 0, //边框线宽
          padding: 5,
          borderRadius: 6,
          axisPointer: {
            lineStyle: {
              color: {
                type: "linear",
                x: 0,
                y: 0,
                x2: 0,
                y2: 1,
                colorStops: [
                  {
                    offset: 0,
                    color: "rgba(255,255,255,0)", // 0% 处的颜色
                  },
                  {
                    offset: 0.5,
                    color: "rgba(255,255,255,1)", // 100% 处的颜色
                  },
                  {
                    offset: 1,
                    color: "rgba(255,255,255,0)", // 100% 处的颜色
                  },
                ],
                global: false, // 缺省为 false
              },
            },
          },
          formatter: function (params) {
            return `<div style="background:#fff;padding: 20px;border-radius: 6px;
box-shadow: 0px 0px 12px 0px rgba(14,23,38,0.1);">
                    <div style="display: flex; align-items: center; margin-bottom: 15px">
                    <span
                    style="
                        width: 12px;
                        height: 12px;
                        background: #3091f2;
                        border-radius: 2px;
                    "
                    ></span>
                    <i style="margin: 0 20px 0 10px; color: #8fa1c9">春夏</i>
                    <i style="color: #8fa1c9">被套</i>
                    <p style="font-weight: 800; margin-left: 40px;color:#111">147000</p>
                </div>
                <div
                    style="
                    display: flex;
                    align-items: center;
                    padding-bottom: 15px;
                    margin-bottom: 15px;
                    border-bottom: 1px solid #ebedf2;
                    "
                >
                    <span
                    style="
                        width: 12px;
                        height: 12px;
                        background: #3091f2;
                        border-radius: 2px;
                        visibility: hidden;
                    "
                    ></span>
                    <i style="margin: 0 20px 0 10px; color: #8fa1c9; visibility: hidden"
                    >春夏</i
                    >
                    <i style="color: #8fa1c9">被套</i>
                    <p style="font-weight: 800; margin-left: 40px;color:#111">147000</p>
                </div>
                <div style="display: flex; align-items: center; margin-bottom: 15px">
                    <span
                    style="
                        width: 12px;
                        height: 12px;
                        background: #2945cb;
                        border-radius: 2px;
                    "
                    ></span>
                    <i style="margin: 0 20px 0 10px; color: #8fa1c9">春夏</i>
                    <i style="color: #8fa1c9">被套</i>
                    <p style="font-weight: 800; margin-left: 40px;color:#111">147000</p>
                </div>
                <div
                    style="
                    display: flex;
                    align-items: center;
                    padding-bottom: 15px;
                    margin-bottom: 15px;
                    border-bottom: 1px solid #ebedf2;
                    "
                >
                    <span
                    style="
                        width: 12px;
                        height: 12px;
                        background: #2945cb;
                        border-radius: 2px;
                        visibility: hidden;
                    "
                    ></span>
                    <i style="margin: 0 20px 0 10px; color: #8fa1c9; visibility: hidden"
                    >春夏</i
                    >
                    <i style="color: #8fa1c9">被套</i>
                    <p style="font-weight: 800; margin-left: 40px;color:#111">147000</p>
                </div>
                <div
                    style="
                    display: flex;
                    align-items: center;
                    justify-content: space-between;
                    "
                >
                    <div>
                    <span
                        style="
                        width: 12px;
                        height: 12px;
                        background: linear-gradient(-90deg, #2945cb, #2a67e4, #3798fd);
                        border-radius: 2px;
                        "
                    ></span>
                    <i style="margin: 0 20px 0 10px; color: #8fa1c9">总计</i>
                    </div>
                    <p style="font-weight: 800; margin-left: 40px;color:#111">147000</p>
                </div>
                </div>`;
          },
        },
        xAxis: [
          {
            type: "category",
            data: ["客户一", "客户二", "客户三", "客户四", "客户五", "客户六"],
            axisTick: {
              show: false, // 是否显示坐标轴轴线
            },
            axisLabel: {
              color: "#282828",
            },
            splitLine: {
              show: false,
            },
            boundaryGap: true,
            axisLine: {
              //坐标轴轴线相关设置。
              show: true,
              inside: false,
              lineStyle: {
                color: "#EBEDF2",
              },
            },
          },
        ],

        yAxis: [
          {
            type: "value",
            min: 0,
            max: 100,
            splitNumber: 5,
            splitLine: { show: false },
            axisLabel: {
              //坐标轴刻度标签的相关设置。
              show: true,
              textStyle: {
                color: "#737373",
              },
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            splitLine: {
              lineStyle: {
                color: "rgba(131,101,101,0.2)",
              },
            },
            show: true,
          },
        ],
        series: [
          {
            type: "bar",
            barMaxWidth: 12,
            zlevel: 10,
            // barGap: '100%',
            data: [40, 79, 50, 40, 79, 50, 40, 79, 50, 40, 79, 50],
            itemStyle: {
              normal: {
                color: {
                  type: "linear",
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "#3798FD",
                    },
                    {
                      offset: 1,
                      color: "#2945CB",
                    },
                  ],
                },
                barBorderRadius: [30, 30, 0, 0],
              },
            },
          },
        ],
      });
    },
  },
};
</script>

<style scoped lang="less"></style>
